package com.baigu.upms.server.controller;

import com.baigu.common.base.BaseController;
import com.baigu.common.util.CookieUtil;
import com.baigu.upms.dao.model.*;
import com.baigu.upms.rpc.api.UpmsApiService;
import com.baigu.upms.rpc.api.UpmsSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 后台controller
 * Created by baigu on 2017/01/19.
 */
@Controller
@RequestMapping("/manage")
@Api(value = "后台管理", description = "后台管理")
public class ManageController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ManageController.class);

	@Autowired
	private UpmsSystemService upmsSystemService;

	@Autowired
	private UpmsApiService upmsApiService;

	@ApiOperation(value = "后台首页")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(ModelMap modelMap, HttpServletResponse response) {
		// 已注册系统
		UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
		upmsSystemExample.createCriteria()
				.andStatusEqualTo((byte) 1);
		List<UpmsSystem> upmsSystems = upmsSystemService.selectByExample(upmsSystemExample);
		modelMap.put("upmsSystems", upmsSystems);
		// 默认为后台管理系统
		UpmsSystem upmsSystem = upmsSystems.stream()
				.filter(s -> s.getName().equals("baigu-admin"))
				.findFirst()
				.get();
		if(upmsSystem != null) {
			CookieUtil.setCookie(response, "baigu-upms-systemid", upmsSystem.getSystemId().toString());
			CookieUtil.setCookie(response, "baigu-upms-systemname", upmsSystem.getName());
			try {
				CookieUtil.setCookie(response, "baigu-upms-systemtitle", URLEncoder.encode(upmsSystem.getTitle(), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
			}
		}
		// 当前登录用户权限
		Subject subject = SecurityUtils.getSubject();
		String username = (String) subject.getPrincipal();
		UpmsUser upmsUser = upmsApiService.selectUpmsUserByUsername(username);
		List<UpmsPermission> upmsPermissions = upmsApiService.selectUpmsPermissionByUpmsUserId(upmsUser.getUserId());
		modelMap.put("upmsPermissions", upmsPermissions);
		return "/manage/index.jsp";
	}

}